package com.yhb.channel.utils;

import lombok.extern.log4j.Log4j;
import lombok.var;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

@Log4j
public class ChannelDateUtil {
    private static SimpleDateFormat defaultDateFormat = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat yearMonthFormat = new SimpleDateFormat("yyyyMM");

    /**
     * 返回离指定日期最近的具体日期
     * @return 最最近的日期
     */
    public static Calendar getFastestDay(int targetDay) {
        Calendar calendar = Calendar.getInstance();
        int todayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // 今天日期
        int day = Math.min(calendar.getActualMaximum(Calendar.DAY_OF_MONTH), targetDay); // 本月最大日期
        if (todayOfMonth > day) {
            calendar.add(Calendar.MONTH, 1);
            day = Math.min(calendar.getActualMaximum(Calendar.DAY_OF_MONTH), targetDay); // 月数 +1 了 所以这里要刷新一下本月最大日期
        }
        calendar.set(Calendar.DAY_OF_MONTH, day);
        return calendar;
    }
    public static Calendar getFastestDay(String dayOfMonth) {
        return getFastestDay(Integer.parseInt(dayOfMonth));
    }

    /**
     * 计算和今天的差距天数
     * @param calendar 日期
     * @return 差距天数
     */
    public static int daysBetweenToday(Calendar calendar) {
        return Period.between(
                LocalDate.now(),
                LocalDate.of(calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH) + 1, // 这里血坑 月份值域是0-11 LocalDate接的是 1 - 12
                        calendar.get(Calendar.DAY_OF_MONTH))).getDays();
    }

    /**
     * 获得dateString
     * @param date 时间
     * @param simpleDateFormat 格式 默认yyyyMMdd
     * @return 格式化字符串
     */
    public static String getDateString(Date date, SimpleDateFormat simpleDateFormat) {
        return simpleDateFormat.format(date);
    }
    public static String getDateString(Date date) {
        return defaultDateFormat.format(date);
    }
    public static String getDateString(Calendar date) {
        return getDateString(date.getTime());
    }

    /**
     * 获取yyyyMM格式化字符串
     * @param date 时间
     * @return
     */
    public static String getMonthDateString(Date date) {
        return yearMonthFormat.format(date);
    }

    public static String getCurrentDateString() {
        return getDateString(new Date());
    }
    public static String getCurrentMonthDateString() {
        return getMonthDateString(new Date());
    }

    public static boolean isBelongToPeriod(Calendar begin, Calendar end) {
        return isBelongToPeriod(Calendar.getInstance(), begin, end);
    }

    /**
     * 获取2020年元旦hour时minute分0秒的Calendar
     * @param hour 时
     * @param minute 分
     * @return Calendar对象
     */
    @NotNull
    public static Calendar getNewYearsDayTimeCalendar(int hour, int minute) {
        var c = Calendar.getInstance();
        c.set(2333, Calendar.JANUARY, 1);
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        return c;
    }

    public static boolean isBelongToPeriod(Calendar theDate, Calendar begin, Calendar end) {
        try {
            return (theDate.compareTo(begin) >= 0 && theDate.compareTo(end) <= 0);
        } catch (Throwable t) {
            log.warn("isBelongToPeriod err:" + theDate + "," + begin + "," + end, t);
            return false;
        }
    }

    /**
     * 获取基于某时间 afterSec后的时间
     * @param base 某时间
     * @param afterSec 多少秒
     * @return 时间对象
     */
    @NotNull
    @Contract("_, _ -> new")
    public static Date getAfterDate(@NotNull Date base, int afterSec) {
        return new Date(base.getTime() + afterSec * 1000);
    }

    /**
     * 获取基于现在 afterSec后的时间
     * @param afterSec 多少秒
     * @return 时间对象
     */
    @NotNull
    @Contract("_ -> new")
    public static Calendar getAfterDate(int afterSec) {
        var c = Calendar.getInstance();
        c.add(Calendar.SECOND, afterSec);
        return c;
    }

    /**
     * 获取过去第past天的日期
     * @param past 过去天数
     * @param format 格式
     * @return 日期
     */
    @NotNull
    public static String getPastDate(int past, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(today);
    }

    /**
     * 获取过去past天内的日期
     * @param intervals 过去天数
     * @param format 格式
     * @return 天数列表
     */
    public static ArrayList<String> getPastDates(int intervals, String format) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        for (int i = 0; i < intervals; i++) {
            pastDaysList.add(getPastDate(i, format));
        }
        return pastDaysList;
    }
    public static void main(String[] args) throws ParseException {
//        System.out.println(getCurrentDateString());
//        System.out.println(isBelongToPeriod("7:00", "22:59"));
//        System.out.println(isBelongToPeriod("00:00", "23:59"));
//        System.out.println(isBelongToPeriod("15:00", "14:00"));
//        System.out.println(isBelongToPeriod("15:01", "15:01"));
    }
}
